<?php

namespace app\studentLayer\controller;

use app\studentLayer\model\StudentModel;
use think\Controller;
use think\Db;
use think\migration\db\Column;

class Student extends Controller
{

    //查询学生表
    public function SelectStudent()
    {

        if ($this->request->isPost()) {

            $Select = $this->request->post();
            $limit = $this->request->post();
            // var_dump($limit);die;
            if (((isset($Select["name"]) && !empty($Select["name"])) || ((isset($Select['c_t_name'])) && !empty($Select['c_t_name'])))) {

                // var_dump(1);die;
                if ($this->request->isAjax()) {
                    //var_dump($Select);die;
                    $Select_Student = StudentModel::SelectStudentModel($Select);
                    $Select_Student = $Select_Student->group('s.id')->select();
                    // var_dump($Select_Student);die;
                    return $this->ajax('', '', $Select_Student);
                }

            } else {

                $Select_Student = StudentModel::SelectStudentModel();
                $list =  $Select_Student;

                $selectStudent = $Select_Student->group('s.id')->page($limit['page'], $limit['limit'])->select();

                $count = $list->group('s.id')->count();

//
//                $list = $Select_Student->group('s.id')->page($limit['page'], $limit['limit'])->select();
//                $Select_Student_count = StudentModel::SelectStudentModel();

                //var_dump($count);die;
                $this->ajax('', '', $selectStudent, $count);

            }

//            学生的基本信息


            //从页面获取的科目和学生姓名
            //$Select = $this->request->post();
            // var_dump($Select);die;
            // $Select_Student = StudentModel::SelectStudentModel($Select);
            //$this->assign('Select_Student', $Select_Student);

//            $this->assign('SelectCourse', $SelectCourse);
//            return $this->fetch('/StudentView/SelectStudent');
        } else {

            //学生的基本信息
//            $Select_Student = StudentModel::SelectStudentModel();
//            $this->assign('Select_Student', $Select_Student);


          //  echo 11; die;

            if ($this->request->isAjax()) {
                $limit = $this->request->get();
//                var_dump($limit);die;
                $Select_Student = StudentModel::SelectStudentModel();
                $Select_Student = $Select_Student->group('s.id')->page($limit['page'], $limit['limit'])->select();

                $Select_Student_count = StudentModel::SelectStudentModel();
                $count = $Select_Student_count->group('s.id')->count();
                //  var_dump($count);die;



                return $this->ajax('', '', $Select_Student, $count);
            }


            return $this->fetch('/StudentView/SelectStudent');
        }

    }


    //学生名字
    public function student_name()
    {

        $studnet = Db::table('s_student')->select();
        $this->ajax('', '', $studnet, '');
    }

    //科目
    public function course()
    {
        $course = Db::table('s_course')->select();
        $this->ajax('', '', $course, '');
    }

    //班级
    public function Classes()
    {
        $Classes = Db::table('s_classes')->select();
        $this->ajax('', '', $Classes, '');
    }


    public function UpStatusStudent()
    {
        $status = $this->request->post();
        $result = Db::table('s_student')->where('id', $status['id'])->update($status);
    }


    public function InsertStudent()
    {

        if ($this->request->isPost()) {

            $Insert = $this->request->post();

            if (!($Insert && isset($Insert['c_id']))) {
                return $this->error('必须选修一个科目', 'Student/InsertStudent');
            }
//            var_dump($Insert);die;
            $result = StudentModel::InsertStudentModel($Insert);
            if ($result) {

                $this->ajax('', '添加成功', $result);
                // return $this->success('添加成功', 'SelectStudent');
            } else {
                $this->ajax('', '添加失败', $result);
//                return $this->error('添加失败', '/StudentView/InsertStudent');
            }

        } else {

//            //科目
//            $Course = Db::table('s_course')->select();
//            $this->assign('Course', $Course);
//
//            //班级
//            $Classes = Db::table('s_classes')->select();
//            $this->assign('Classes', $Classes);

            return $this->fetch('/StudentView/InsertStudent');
        }


    }


    public function UpdateStudent()
    {

        if ($this->request->isPost()) {

            $Update = $this->request->post();
            // var_dump($Update);die;
            if (!($Update && isset($Update['c_id']))) {
                return $this->error('必须选修一个科目', 'Student/SelectStudent');
            }

            $result = StudentModel::UpdateStudentModel($Update);
            if ($result) {
                return $this->success('修改成功', 'SelectStudent');
            } else {
                return $this->error('修改失败', 'Student/SelectStudent');
            }


        } else {

            $SelectId = $this->request->get();

            $UpdateData = Db::table('s_student')->alias('s')
                ->field('s.*,c.id as c_id ,c.name as c_name,cls.id as cls_id,cls.name as cls_name')
                ->leftJoin('s_report r', 's.id = r.s_id')
                ->leftJoin('s_course c', 'c.id = r.c_id')
                ->leftJoin('s_classes cls', 'cls.id = r.cls_id')
                ->where(['s.id' => $SelectId])->select();

            //科目id，表示学生选择了多少个科目
            $c_id = array_column($UpdateData, 'c_id', 'c_id');

            $UpdateData = array_column($UpdateData, NULL, 'id');
            $UpdateData = array_values($UpdateData);

            //var_dump($UpdateData);die;
            //二维数组取出 一维数组
            $UpdateData = $UpdateData['0'];
            unset($UpdateData['c_id'], $UpdateData['c_name']);

            // var_dump($UpdateData,$c_id);die;

            $this->assign('UpdateData', $UpdateData);
            //学生选定的科目id
            //$this->assign('c_id',$c_id);


            if ($this->request->isAjax()) {

                $UpdateData['c_id'] = $c_id;
                return $this->ajax('', '', $UpdateData);
            }
            return $this->fetch('StudentView/UpdateStudent');
        }


    }


    public function DeleteStudent()
    {
        $Id = $this->request->get();
        $result = StudentModel::DeleteStudent($Id);
        if ($result) {
            return $this->success('删除成功', 'SelectStudent');
        } else {
            return $this->error('删除失败', 'Student/SelectStudent');
        }


    }

    public function ajax(string $code, string $msg, $data = [], $count = NULL)
    {
        $ajax = [
            'code' => $code ? $code : 0,
            'msg' => $msg ? $msg : 'null',
            'data' => $data,
            'count' => $count
        ];
        // _json();
       // var_dump(json_encode($ajax));die;
        echo(json_encode($ajax));die;

       // json_encode($ajax);die;
    }


}